package fr.uparis10.miage.m1p1.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
//import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Level;

import fr.uparis10.miage.classic.m1.p3.Departement;
//import fr.uparis10.miage.m1p1.core.Employe;
//import fr.uparis10.miage.m1p1.core.Projet;

public class DepartementReader extends PropertyReader {
	private static DepartementReader theInstance = null;

	/**
	 * Private Constructor - singleton class
	 */
	private DepartementReader() {
	}

	public static DepartementReader getInstance() {
		if (theInstance == null) {
			theInstance = new DepartementReader();
		}
		return theInstance;
	}

	public ArrayList<Departement> readDepartement(File aFile) {
		Properties prop = new Properties();
		FileInputStream inStream;

		ArrayList<Departement> departementListe = new ArrayList<Departement>();
		try {
			inStream = new FileInputStream(aFile);
			prop.load(inStream);
		} catch (FileNotFoundException exc) {
			log.log(Level.SEVERE, "Exception occured: ", exc);
			return departementListe;
		} catch (IOException exc) {
			log.log(Level.SEVERE, "Exception occured: ", exc);
			return departementListe;
		}
		int deptCount = getIntProperty(prop, "departement_count", -1);
		if (deptCount <= 0) {
			log.log(Level.SEVERE, "Invalid employe_count=" + deptCount
					+ ". Return...");
			return departementListe;
		}
		String deptName;
//		ArrayList<Projet> proj = null;
//		ArrayList<Projet> projDept = null;
//		boolean exist = false;
		// debut de lecture de données departement
		for (int i = 0; i < deptCount; i++) {
			/* Start to read & convert */
			deptName = getStringProperty(prop, "dept_nom_" + i, "");
			Departement empDept = new Departement(deptName);
			/* End of read & convert */

			/* Create the Employe object */
//			EmployeReader empr = EmployeReader.getInstance();
//			ArrayList<Employe> emp = new ArrayList<Employe>();
//			File f = new File("Employe.properties");
//			emp = empr.readEmployes(f);
//			for (Employe e : emp) {
//				if (empDept.equals(e.getDepartement())) {
//					empDept.addEmploye(e);
//					proj = e.getProjetList();
//					for (Projet p : proj) {
//						projDept = empDept.getProjet();
//						for (Projet pr : projDept)
//							if (exist = p.equals(pr))
//								break;
//						if (!exist)
//							empDept.addProjet(p);
//					}
//				}
//			}

			departementListe.add(empDept);
//			departementMap.put(deptName, empDept);

		}

		return departementListe;

	}
}
